//
//  WJShowPictureVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJShowPictureVC.h"
#import "DALabeledCircularProgressView.h"
#import "WJEssAndNewModel.h"
#import "WJEssAndNewButton.h"
#import "SVProgressHUD.h"

@interface WJShowPictureVC ()

//  加载的圆形宽
@property (nonatomic, strong) DALabeledCircularProgressView *progressView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) WJBaseImageView *imageView;

@property (nonatomic, strong) WJEssAndNewButton *backButton;

@property (nonatomic, strong) WJEssAndNewButton *saveButton;
// 评论的按钮
@property (nonatomic, strong) WJEssAndNewButton *plButton;

@end

@implementation WJShowPictureVC

-(void)dealloc
{
    [_model release];
    [_progressView release];
    [_scrollView release];
    [_imageView release];
    [_backButton release];
    [_saveButton release];
    [_plButton release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    [self createView];
}

- (void)createView
{
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 24, WIDTH, HEIGHT)];
    
    [self.view addSubview:self.scrollView];
    
    self.scrollView.backgroundColor = [UIColor colorWithRed:16/255.0 green:16/255.0 blue:16/255.0 alpha:1];
    
    self.backButton =[WJEssAndNewButton buttonWithTitle:@"<" frame: CGRectMake(20, 40, 30, 30) target:self action:@selector(back)];
    self.backButton.layer.cornerRadius = 15;
    self.backButton.layer.masksToBounds = YES;
    [self.backButton setTitleColor:[UIColor whiteColor] forState:0];
    self.backButton.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.backButton];
    
    self.saveButton = [WJEssAndNewButton buttonWithTitle:@"保存" frame:CGRectMake(20, HEIGHT - 40, 50, 30) target:self action:@selector(save)];
    self.saveButton.backgroundColor = [UIColor grayColor];
    self.saveButton.layer.cornerRadius = 3;
    [self.view addSubview:self.saveButton];
    [self.saveButton setTitleColor:[UIColor whiteColor] forState:0];

    
    
    self.plButton = [WJEssAndNewButton buttonWithTitle:nil frame:CGRectMake(WIDTH - 60,  HEIGHT - 40, 50, 30) target:self action:@selector(backPLVC)];
    [self.plButton setImage:[UIImage imageNamed:@"mainCellComment"] forState:0];
    self.plButton.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.plButton];
    self.plButton.layer.cornerRadius = 3;
    [self.plButton setTitleColor:[UIColor whiteColor] forState:0];

    
    //添加图片
     self.imageView = [[WJBaseImageView alloc]init];
    
    //设置图片能与用户交互
     self.imageView.userInteractionEnabled = YES;
    
    //给图片添加点击手势
    [ self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)]];
    
    [self.scrollView addSubview: self.imageView];
    
    //图片尺寸
    CGFloat pictureW = WIDTH;
    CGFloat pictureH;
    if ([self.model.type isEqualToString:@"image"]) {
        
//        NSNumber *height = self.model.image[@"height"];
//        
//        NSNumber *width = self.model.image[@"width"];
        
        pictureH = self.model.imageModel.height * (WIDTH - 20) / self.model.imageModel.width;

    }else if ([self.model.type isEqualToString:@"gif"]){
        
//        NSNumber *height = self.model.gif[@"height"];
//        
//        NSNumber *width = self.model.gif[@"width"];
        
        pictureH = self.model.gifModel.height * (WIDTH - 20) / self.model.gifModel.width;

    }else if ([self.model.type isEqualToString:@"audio"]){
        
//        NSNumber *height = self.model.audio[@"height"];
//        
//        NSNumber *width = self.model.audio[@"width"];
        
        pictureH = self.model.audioModel.height * (WIDTH - 20) / self.model.audioModel.width;

    }
    
    if (pictureH > HEIGHT) {//图片显示的高度超过一个屏幕，需要滚动查看
        
        self.imageView.frame = CGRectMake(0, 0, pictureW, pictureH);
        self.scrollView.contentSize = CGSizeMake(0, pictureH + 24);
        
    }else{
        
        self.imageView.frame = CGRectMake(0, 0, WIDTH, pictureH);
        self.imageView.center = self.view.center;
        
    }
    
    //马上显示当前图片的下载进度
    [self.progressView setProgress:self.model.pictureProgress animated:YES];
    
    //下载图片
    NSString *url;
    if ([self.model.type isEqualToString:@"image"]) {
        url = self.model.image[@"download_url"][0];
    }else if ([self.model.type isEqualToString:@"gif"]){
        url = self.model.gif[@"images"][0];
    }else if ([self.model.type isEqualToString:@"audio"]){
        url = self.model.audio[@"download_url"][0];
    }
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"defaultTagIcon~iphone"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        [self.progressView setProgress:1.0 * receivedSize / expectedSize animated:NO];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden = YES;
    }];
    
}

- (void)back
{
     [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)save
{
    if (self.imageView.image == nil) {
        [SVProgressHUD showErrorWithStatus:@"图片并未下载完毕"];
        return;
    }
    
    /**************** 将图片写入相册 ****************/
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}

#pragma marj - 返回评论页面
- (void)backPLVC
{
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
